package logic

import (
	"cart-go/cartrpc/cartrpc"
	"cart-go/cartrpc/internal/svc"
	"cart-go/model"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodAddLogic) GoodAdd(in *cartrpc.GoodAddRequest) (*cartrpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line
	var Good model.Good
	model.DB.Debug().Where("id=?", in.GoodID).Find(&Good)
	if in.GoodID == 0 {
		return nil, errors.New("商品不存在,请添加")
	}
	good := model.Good{
		Name:     in.Name,
		Price:    float64(in.Price),
		Status:   in.Status,
		Stock:    in.Stock,
		Total:    float64(in.Total),
		Quantity: in.Quantity,
	}

	model.DB.Debug().Create(&good)
	return &cartrpc.GoodAddResponse{
		Success: "商品添加成功",
		Message: true,
	}, nil
}
